<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
      <el-form-item label="终端imei号" prop="termImei" width="30%">
        <el-input
          v-model.trim="queryParams.termImei"
          placeholder="请输入终端的imei号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="终端类别" prop="termKind" width="30%">
        <el-select v-model="queryParams.termKind" placeholder="请选择终端类别" clearable size="small">
          <el-option
          v-for="dict in termKindOptions"
          :key="dict.dictValue"
          :label="dict.dictLabel"
          :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['terminal:attr:add']"
        >录入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['terminal:attr:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['terminal:attr:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['terminal:attr:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="attrList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="45px" align="center" />
      <el-table-column label="终端imei号" align="center" prop="termImei" min-width="160%"/>
      <el-table-column label="终端sim卡的iccid号" align="center" prop="simIccid" min-width="200%"/>
      <el-table-column label="终端类别" align="center" prop="termKind" :formatter="termKindFormat" min-width="120%"/>
      <el-table-column label="终端型号编号" align="center" prop="termModel" min-width="120%"/>
      <el-table-column label="终端生产商编号" align="center" prop="termFac" min-width="200%"/>
      <el-table-column label="终端提供商编号" align="center" prop="termPrvdr" min-width="200%"/>
      <el-table-column label="录入操作员编码" align="center" prop="opr" min-width="180%"/>
      <el-table-column label="创建时间" align="center" prop="gmtCreate" min-width="160%">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.gmtCreate, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center" prop="gmtModified" min-width="160%">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.gmtModified, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" min-width="130%">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['terminal:attr:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['terminal:attr:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改属性管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-position="left">
        <el-form-item label="终端imei号" prop="termImei" label-width="92px" v-if="showImei">
          <el-input v-model.trim="form.termImei" placeholder="请输入终端的imei号" />
        </el-form-item>
        <el-form-item label="终端iccid号" prop="simIccid" label-width="92px">
          <el-input v-model.trim="form.simIccid" placeholder="请输入终端sim卡的iccid号" />
        </el-form-item>
        <el-form-item label="终端类别" prop="termKind" label-width="92px">
          <el-select v-model="form.termKind" placeholder="请选择终端类别">
            <el-option
            v-for="dict in termKindOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="终端型号编号" prop="termModel" label-width="112px">
          <el-input v-model.trim="form.termModel" placeholder="请输入终端型号编号" />
        </el-form-item>
        <el-form-item label="终端生产商编号" prop="termFac" label-width="112px">
          <el-input v-model.trim="form.termFac" placeholder="请输入终端生产商编号" />
        </el-form-item>
        <el-form-item label="终端提供商编号" prop="termPrvdr" label-width="112px">
          <el-input v-model.trim="form.termPrvdr" placeholder="请输入终端提供商编号" />
        </el-form-item>
        <el-form-item label="录入操作员编码" prop="opr" label-width="112px">
          <el-input v-model.trim="form.opr" placeholder="请输入录入操作员编码" />
        </el-form-item>
        <el-form-item label="创建时间" prop="gmtCreate" v-if="showCreate">
          <el-input v-model="form.gmtCreate" placeholder="" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAttr, getAttr, delAttr, addAttr, updateAttr } from "@/api/terminal/attr";

export default {
  name: "Attr",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 表单默认显示imei
      showImei: true,
      // 表单默认不显示创建时间
      showCreate: false,
      // 总条数
      total: 0,
      // 属性管理表格数据
      attrList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 终端类别字典
      termKindOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        termImei: null,
        termKind: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        termImei: [
           { required: true, message: "终端imei号不能为空", trigger: "blur" },
           { max: 16, message: "终端imei号长度不能超过15位", trigger: "blur" }
        ],
        simIccid: [
           { max: 20, message: "终端sim卡的iccid号不能超过20位", trigger: "blur" }
        ],
        termModel: [
           { type: "number", max: 127, message: "终端型号编号不能超过127", trigger: "blur", transform: (value) => Number(value) }
        ],
        termFac: [
           { max: 20, message: "终端生产商编号不能超过20位", trigger: "blur" }
        ],
        termPrvdr: [
           { max: 20, message: "终端提供商编号不能超过20位", trigger: "blur" }
        ],
        opr: [
           { max: 18, message: "录入操作员编码不能超过18位", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("term_kind").then(response => {
      this.termKindOptions = response.data;
    });
  },
  methods: {
    /** 查询属性管理列表 */
    getList() {
      this.loading = true;
      listAttr(this.queryParams).then(response => {
        this.attrList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 终端类别字典翻译
    termKindFormat(row, column) {
      return this.selectDictLabel(this.termKindOptions, row.termKind);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        termImei: null,
        simIccid: null,
        termKind: null,
        termModel: null,
        termFac: null,
        termPrvdr: null,
        opr: null,
        gmtCreate: null,
        gmtModified: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.termImei)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.showImei = true;
      this.title = "录入新设备的属性";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const termImei = row.termImei || this.ids
      getAttr(termImei).then(response => {
        this.form = response.data;
        this.open = true;
        this.showImei = false;
        this.title = '修改终端imei号为"' + termImei + '"的属性';
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.gmtCreate != null) {
            updateAttr(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            getAttr(this.form.termImei).then(response => {
              if(response.data == null){
                addAttr(this.form).then(response => {
                  this.msgSuccess("录入成功");
                  this.open = false;
                  this.getList();
                });
              } else if(response.data != null) {
                this.msgError("给设备已存在");
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const termImeis = row.termImei || this.ids;
      this.$confirm('是否确认删除终端imei号为"' + termImeis + '"的终端信息?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delAttr(termImeis);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('terminal/attr/export', {
        ...this.queryParams
      }, `terminal_attr.xlsx`)
    }
  }
};
</script>
